{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Illustrates `numpy` vs `einsum`\n",
    "In deep learning, we perform a lot of tensor operations. `einsum` simplifies and unifies the APIs for these operations.\n",
    "\n",
    "`einsum` can be found in numerical computation libraries and deep learning frameworks.\n",
    "Let us demonstrate how to import and use `einsum` in `numpy` and PyTorch. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "w:\n",
      " [[0. 1. 2.]\n",
      " [3. 4. 5.]]\n",
      "x:\n",
      " [[1.]\n",
      " [1.]\n",
      " [1.]]\n",
      "y:\n",
      " [[ 3.]\n",
      " [12.]]\n",
      "y:\n",
      " [[ 3.]\n",
      " [12.]]\n",
      "y:\n",
      " tensor([[ 3.],\n",
      "        [12.]])\n"
     ]
    }
   ],
   "source": [
    "w = np.arange(6).reshape(2,3).astype(np.float32)\n",
    "x = np.ones((3,1), dtype=np.float32)\n",
    "\n",
    "print(\"w:\\n\", w)\n",
    "print(\"x:\\n\", x)\n",
    "\n",
    "y = np.matmul(w, x)\n",
    "print(\"y:\\n\", y)\n",
    "\n",
    "#y = einsum('ij,jk->ik', torch.from_numpy(w), torch.from_numpy(x))\n",
    "y = np.einsum('ij,jk->ik', w, x)\n",
    "print(\"y:\\n\", y)\n",
    "\n",
    "y = torch.einsum('ij,jk->ik', torch.from_numpy(w), torch.from_numpy(x))\n",
    "print(\"y:\\n\", y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Tensor multiplication with transpose in `numpy` and `einsum`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "w:\n",
      " [[0. 1. 2.]\n",
      " [3. 4. 5.]]\n",
      "x:\n",
      " [[1. 1. 1.]]\n",
      "y:\n",
      " [[ 3.]\n",
      " [12.]]\n",
      "y:\n",
      " [[ 3.]\n",
      " [12.]]\n",
      "y:\n",
      " tensor([[ 3.],\n",
      "        [12.]])\n"
     ]
    }
   ],
   "source": [
    "w = np.arange(6).reshape(2,3).astype(np.float32)\n",
    "x = np.ones((1,3), dtype=np.float32)\n",
    "\n",
    "print(\"w:\\n\", w)\n",
    "print(\"x:\\n\", x)\n",
    "\n",
    "y = np.matmul(w, np.transpose(x))\n",
    "print(\"y:\\n\", y)\n",
    "\n",
    "y = np.einsum('ij,kj->ik', w, x)\n",
    "print(\"y:\\n\", y)\n",
    "\n",
    "y = torch.einsum('ij,kj->ik', torch.from_numpy(w), torch.from_numpy(x))\n",
    "print(\"y:\\n\", y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Properties of square matrices in `numpy` and `einsum`\n",
    "\n",
    "We demonstrate diagonal."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "w:\n",
      " [[0. 1. 2.]\n",
      " [3. 4. 5.]\n",
      " [6. 7. 8.]]\n",
      "d:\n",
      " [0. 4. 8.]\n",
      "d:\n",
      " [0. 4. 8.]\n",
      "d:\n",
      " tensor([0., 4., 8.])\n"
     ]
    }
   ],
   "source": [
    "w = np.arange(9).reshape(3,3).astype(np.float32)\n",
    "d = np.diag(w)\n",
    "print(\"w:\\n\", w)\n",
    "print(\"d:\\n\", d)\n",
    "d = np.einsum('ii->i', w)\n",
    "print(\"d:\\n\", d)\n",
    "d = torch.einsum('ii->i', torch.from_numpy(w))\n",
    "print(\"d:\\n\", d)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Trace."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "t:\n",
      " 12.0\n",
      "t:\n",
      " 12.0\n",
      "t:\n",
      " tensor(12.)\n"
     ]
    }
   ],
   "source": [
    "t = np.trace(w)\n",
    "print(\"t:\\n\", t)\n",
    "\n",
    "t = np.einsum('ii->', w)\n",
    "print(\"t:\\n\", t)\n",
    "t = torch.einsum('ii->', torch.from_numpy(w))\n",
    "print(\"t:\\n\", t)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Sum along an axis."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "s:\n",
      " [ 9. 12. 15.]\n",
      "s:\n",
      " [ 9. 12. 15.]\n",
      "s:\n",
      " tensor([ 9., 12., 15.])\n"
     ]
    }
   ],
   "source": [
    "s = np.sum(w, axis=0)\n",
    "print(\"s:\\n\", s)\n",
    "\n",
    "s = np.einsum('ij->j', w)\n",
    "print(\"s:\\n\", s)\n",
    "s = torch.einsum('ij->j', torch.from_numpy(w))\n",
    "print(\"s:\\n\", s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let us demonstrate tensor transpose. We can also use `w.T` to transpose `w` in numpy."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "t:\n",
      " [[0. 3. 6.]\n",
      " [1. 4. 7.]\n",
      " [2. 5. 8.]]\n",
      "t:\n",
      " [[0. 3. 6.]\n",
      " [1. 4. 7.]\n",
      " [2. 5. 8.]]\n",
      "t:\n",
      " tensor([[0., 3., 6.],\n",
      "        [1., 4., 7.],\n",
      "        [2., 5., 8.]])\n"
     ]
    }
   ],
   "source": [
    "t = np.transpose(w)\n",
    "print(\"t:\\n\", t)\n",
    "\n",
    "t = np.einsum(\"ij->ji\", w)\n",
    "print(\"t:\\n\", t)\n",
    "t = torch.einsum(\"ij->ji\", torch.from_numpy(w))\n",
    "print(\"t:\\n\", t)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Dot, inner and outer products in `numpy` and `einsum`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a:\n",
      " [1. 1. 1.]\n",
      "b:\n",
      " [2. 2. 2.]\n",
      "d:\n",
      " 6.0\n",
      "d:\n",
      " 6.0\n",
      "d:\n",
      " tensor(6.)\n",
      "i:\n",
      " 6.0\n",
      "i:\n",
      " 6.0\n",
      "i:\n",
      " tensor(6.)\n",
      "o:\n",
      " [[2. 2. 2.]\n",
      " [2. 2. 2.]\n",
      " [2. 2. 2.]]\n",
      "o:\n",
      " [[2. 2. 2.]\n",
      " [2. 2. 2.]\n",
      " [2. 2. 2.]]\n",
      "o:\n",
      " tensor([[2., 2., 2.],\n",
      "        [2., 2., 2.],\n",
      "        [2., 2., 2.]])\n"
     ]
    }
   ],
   "source": [
    "a = np.ones((3,), dtype=np.float32)\n",
    "b = np.ones((3,), dtype=np.float32) * 2\n",
    "\n",
    "print(\"a:\\n\", a)\n",
    "print(\"b:\\n\", b)\n",
    "\n",
    "d = np.dot(a,b)\n",
    "print(\"d:\\n\", d)\n",
    "d = np.einsum(\"i,i->\", a, b)\n",
    "print(\"d:\\n\", d)\n",
    "d = torch.einsum(\"i,i->\", torch.from_numpy(a), torch.from_numpy(b))\n",
    "print(\"d:\\n\", d)\n",
    "\n",
    "i = np.inner(a, b)\n",
    "print(\"i:\\n\", i)\n",
    "i = np.einsum(\"i,i->\", a, b)\n",
    "print(\"i:\\n\", i)\n",
    "i = torch.einsum(\"i,i->\", torch.from_numpy(a), torch.from_numpy(b))\n",
    "print(\"i:\\n\", i)\n",
    "\n",
    "o = np.outer(a,b)\n",
    "print(\"o:\\n\", o)\n",
    "o = np.einsum(\"i,j->ij\", a, b)\n",
    "print(\"o:\\n\", o)\n",
    "o = torch.einsum(\"i,j->ij\", torch.from_numpy(a), torch.from_numpy(b))\n",
    "print(\"o:\\n\", o)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "agents",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
